<?php 
/**
 * @filesource upload.func.php
 *   上传图片, 需要 ImageMagick
 * 
 */

/**
 * 上传文件
 *
 * 返回值:
 *  0 文件类型错误
 */
function upFile($size){
	set_time_limit(0);
	$fileType = array("jpg","gif","bmp","jpeg","png");
	$upPath = dirname(dirname(dirname(dirname(__FILE__))))
		.DIRECTORY_SEPARATOR.'pico'.DIRECTORY_SEPARATOR; 
		//dump( $_FILES );
	$a = strtolower( pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION) );
	//判断文件类型
	if(!in_array( $a, $fileType )) {
		//$text=implode(",",$fileType);
    	return 2; //echo "您只能上传以下类型文件: ",$text,"<br>";
	} else{ //生成目标文件的文件名   
   		$filename=explode(".", $_FILES['file']['name']);
   	
	   	do{ 
	   		$filename[0]=randomFilename(16); //设置随机数长度
	     	$name=implode(".", $filename);
	     	$uploadfile= $upPath.$name;
	   }while( file_exists($uploadfile) );
	
	   try {
	   	if ( move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile) ){
			///usr/bin/convert
			exec("convert -resize '{$size}>!' {$uploadfile} {$uploadfile}");
	       	return '/notice/pico/'.$name;
	   		}
	   }catch (Exception $e){
	   		echo $e->getMessage();
	   }
	}
	return 0;
}//END func upFile

//生成随机文件名函数   
function randomFilename($length){
	$hash = 'kkyoo-ld-';
	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
	$max = strlen($chars) - 1;
	mt_srand((double)microtime() * 1000000);
	for($i = 0; $i < $length; $i++){
		$hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}
?>